package com.huazi.www.yang.fchua.servlet;

import com.alibaba.fastjson.JSON;
import com.huazi.www.yang.fchua.entity.Medicine;
import com.huazi.www.yang.fchua.entity.Prescription;
import com.huazi.www.yang.fchua.service.BillService;
import com.huazi.www.yang.fchua.service.Imlp.BillServiceImpl;
import com.huazi.www.yang.fchua.uilt.Page;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * 药单的缴费
 */
public class BillServlet extends HttpServlet {
    BillService bill = new BillServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        String tp = req.getParameter("tp");
        if (tp == null) {
            tp = "select";
        }
        if (tp.equals("select")) {
            String userName = req.getParameter("userName");
            if (userName == null || userName.equals("")) {
                userName = "";
            }
            String currentPage = req.getParameter("currentPage");
            if (currentPage == null || currentPage.equals("")) {
                currentPage = "1";
            }
            int curr = Integer.parseInt(currentPage);
            Page<Medicine> page = bill.page(userName, curr);
            req.setAttribute("page", page);
            req.getRequestDispatcher("frontdesk/fchua/guide.jsp").forward(req, resp);
        } else if (tp.equals("insert")) {    //用户缴费

            String userid = req.getParameter("userid");    //用户编号
            String date = req.getParameter("date");        //开药时间

            //查询药单集合 list()
            List<Medicine> list = bill.list(userid, date);

            //得到药单的总价格 money()
            double money = bill.money(list);

            //得到判断结果并缴费
            boolean flag = bill.pdMoney(userid, money);

            if (flag) {
                //药名+数量
                String drugs = bill.drugs(list);
                //缴费实体
                Prescription pr = new Prescription(Integer.parseInt(userid), drugs, money);
                //新增缴费记录
                bill.insert(pr);
                //更新药单状态
                bill.update(list);
            }
            //输出
            PrintWriter out = resp.getWriter();
            out.print(flag);
            out.flush();
            out.close();
        } else if (tp.equals("details")) {
            String userid = req.getParameter("userid");     //用户编号
            String date = req.getParameter("date");         //开药时间
            List<Medicine> list = bill.list(userid, date);     //查询药单集合 list()
            PrintWriter out = resp.getWriter();
            String s = JSON.toJSONString(list);
            out.print(s);
            out.flush();
            out.close();
        } else if (tp.equals("money")) {    //返回药单的总价格
            String userid = req.getParameter("userid");    //用户编号
            String date = req.getParameter("date");        //开药时间
            //查询药单集合 list()
            List<Medicine> list = bill.list(userid, date);
            //得到药单的总价格 money()
            double money = bill.money(list);
            //输出流
            PrintWriter out = resp.getWriter();
            //转JSON格式
            String s = JSON.toJSONString(money);
            //输出
            out.print(s);
            //刷新缓存区
            out.flush();
            //关闭流
            out.close();
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }

}
